package com.example.esdemo.controller;

import com.example.esdemo.entity.Book;
import com.example.esdemo.entity.ESBook;
import com.example.esdemo.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2023-04-12
 */
@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {
    @Autowired
    private BookService bookService;

    //MySQL->es
    @GetMapping("load")
    public void load(){
        //取出所有的数据库数据
        List<Book> list = bookService.list();
        for (Book book : list) {
            log.info("book:{}",book);
        }
        //把数据加入到es
        bookService.add2ES(list);
    }

    @GetMapping("findAll/{keywords}")
    public List<ESBook> findAll(@PathVariable String keywords){
        List<ESBook> list = bookService.findAll(keywords);
        return list;
    }
}
